package com.catering.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/alipay")
public class AlipayController {

    @GetMapping("/pay")
    public  void  pay(HttpServletResponse servletResponse, String outTradeNo) throws Exception {
        //支付宝接口配置
        Config config=new Config();
        //网关
        config.protocol="https";
        config.gatewayHost="openapi-sandbox.dl.alipaydev.com";
        //密钥
        config.merchantPrivateKey="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC3ND235EHv3YKR+mzil7HR8N0NZjR+tj7p/e9GpK/k8mEFepDoalMm/qPrN4wCFjpRAhOF0kmnOJMGtC+8Wwa5C0aqy70viO0yAl/sMueZ4TBHndUm7vyUxi0V2gL9AcSaBcNaXMPecMCu83A3G7DleIwpNrcChrDU89VxnCjTFJhqYdSbPv2vONQKhB3bQ4TzCo8HhMysp694piju9RPJQoA/sIEVG2OZoZbTY9iGHPRW0S3AtrTFnQL1tZLyKy6r6l1V3rfTM47Vn7wz8zgHRM/8GAp4mgNwjfPFp0w96WTL1KfHkEWKQxrNm9o11lO17jsO+q+tSv1Zfecr+TpZAgMBAAECggEAR58nxb15vaF2i0VAzjEJto6YjMV5HlR3MhkxMwftL+9F2NN61THEAkmooatck/BGqibKpRV57+LhL8y/oL5hWXQ8E0eFVZENUfC1uWyJOJJdwpHEo+zoV+1F8PYDFxBa4PAB2FDNzid8XKkVJKYxuPQwr2gRke5AMXCF8F1hZGtMjdG60VPxjCVC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9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0hNMkNoQ+r/zpiqj4WUjTqHdSkJEqcUH26bFt1lOQnkdtg8VUmnEaIpcVrbvuqBVXEMjiQ51voEjtyhUYb9i7PXHXeTJOg5xMDFcu97HegfTf/B1Pe1oPcG+zYFydHOFeWkOELWDrk9RUIEqX1Yf4C+7iAiBVqDMOm/dcv14stuQ8PM7TKXZ0Vq0p70yVGd8SxP5XIJfy5wXbTMrxfQWPJl8Lt2bqc4Ff+xpaLpCb/d378dzmfYGMqrBme3VuVI6iTIUMqPuU+dZ+ueyUnFhez3Umn15/9DpQOU3byLOwZ7GgftBA7oABfOT8Y6OLPdJKmY0UEpusmASZZ3o4BTOIQIDAQAB";
        //签名类型
        config.signType="RSA2";
        //appid
        config.appId="9021000131612271";
        //支付宝回调notifyUrl
        config.notifyUrl="http://localhost:8080/alipay/notify";

        Factory.setOptions(config);

        //发起调用支付宝接口
        //商品信息,显示在付款页面,订单编号,支付金额
        AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate("ipone16 1t 256", outTradeNo, "1000");

        //响应解析
        String httpBody = response.getHttpBody();
        System.out.println("响应结果：");
        System.out.println(httpBody);

        //获取二维码信息
        JSONObject jsonObject = JSONUtil.parseObj(httpBody);
        String qrUrl = jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();

        //生成二维码
        // 生成指定url对应的二维码到文件，宽和高都是300像素
//        QrCodeUtil.generate(qrUrl, 300, 300, FileUtil.file("d:/qrcode.jpg"));

        //设置响应servletResponse
        ServletOutputStream out = servletResponse.getOutputStream();
        QrCodeUtil.generate(qrUrl, 300, 300,"",out);
        out.close();
    }

    @PostMapping("/notify")
    public  void  notifyUrl(){
        System.out.println("回调。。。。。。");
//        获取支付结果
//        根据结果进行业务处理：修改订单的状态
    }

    @GetMapping("/queryPay")
    public  String  queryPay(String outTradeNo) throws Exception {
        //支付宝接口配置
        Config config=new Config();
        //网关
        config.protocol="https";
        config.gatewayHost="openapi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yuDiTHWWOugf/U1kAx5sLJLjL2Brkis6vCqftgLxbZAPzwq68GOXwKBgQDiAzA6T8HTkSDGaKBW2LwzVGKzMwPb9J7xlpoUocnV4kHC8bEUWa8vr+NzzM+d+kGRN7LYpoaPS2f3E0LplQ8rWhEJ3zr0mQE31onmxvbLtbI0eaVfh3Mv2kF3ksOdCOCQnn+nNIe8Si0WIWq9bvU0uC0j4+X2Chuml+vcMGF7bQKBgQCqrgVXl+L+6BOndpjP4Lem182T+M0VXKg9+MJu7mOlTeeeDT3QD+6oIHS+yQYFegPeoCv8F1av9GdRtRoNsXpxfDBz7RhHwxMpEuan3yAndEMzT+fQ9yRR1MXjmHoPGB2bSmHSl462mExjqCmhfZucww2KSPmNn7KiHs3zzVnkJQKBgArOfGKmDhSkvepJ54DZIav3HLQ5VLtbfgwkv9N2P9v1aLiCwUFeZ/EUhFqJV2Y7a62mTROMZwx5pWF9n6n2DrL8UonrPwFHxwKi7OQzApOEm+8USLMzrH1bFh7c2MWgbF6P4LWOjRSAWzQvAUGhDD9vhXjn8ilhhJF8gNMtY60K";
        config.alipayPublicKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0hNMkNoQ+r/zpiqj4WUjTqHdSkJEqcUH26bFt1lOQnkdtg8VUmnEaIpcVrbvuqBVXEMjiQ51voEjtyhUYb9i7PXHXeTJOg5xMDFcu97HegfTf/B1Pe1oPcG+zYFydHOFeWkOELWDrk9RUIEqX1Yf4C+7iAiBVqDMOm/dcv14stuQ8PM7TKXZ0Vq0p70yVGd8SxP5XIJfy5wXbTMrxfQWPJl8Lt2bqc4Ff+xpaLpCb/d378dzmfYGMqrBme3VuVI6iTIUMqPuU+dZ+ueyUnFhez3Umn15/9DpQOU3byLOwZ7GgftBA7oABfOT8Y6OLPdJKmY0UEpusmASZZ3o4BTOIQIDAQAB";
        //签名类型
        config.signType="RSA2";
        //appid
        config.appId="9021000131612271";
        //支付宝回调notifyUrl
//        config.notifyUrl="http://localhost:8080";
        Factory.setOptions(config);


//        查询支付结果
        AlipayTradeQueryResponse queryResponse = Factory.Payment.Common().query(outTradeNo);
        String httpBody = queryResponse.getHttpBody();
        System.out.println(httpBody);

        //解析支付结果
        JSONObject jsonObject = JSONUtil.parseObj(httpBody);
        String trade_status = jsonObject.getJSONObject("alipay_trade_query_response").get("trade_status").toString();

        return trade_status;
    }
}

